package cn.example.office_auto_sys.modules.login.controller;

import cn.example.office_auto_sys.common.rseult.CommonResult;
import cn.example.office_auto_sys.modules.BaseController;
import cn.example.office_auto_sys.modules.login.entity.User;
import cn.example.office_auto_sys.modules.login.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.CallNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

/**
 * @Account :LoginController.java
 * @Author :T016729
 * @Date :2022/6/22
 * @Description :登陆功能接口
 */
@Api(tags = "用户登陆注册功能接口")
@Controller
public class LoginController extends BaseController {
    /**
     * 登录界面的显示
     *
     * @return
     */
    @ApiOperation("")
    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @Autowired
    private UserService userService;

    @ApiOperation("用户登陆验证")
    @PostMapping("/login")
    @ResponseBody
    public CommonResult loginCheck(@RequestParam("username") String username, @RequestParam("password") String password) {
        User user = userService.userLogin(username.trim(), password.trim());
        if (isEmpty(user)) {
            return CommonResult.failed("用户名或密码错误");
        }
        return CommonResult.success("登陆成功!");
    }

    @ApiOperation("用户注册")
    @PostMapping("/register ")
    @ResponseBody
    public CommonResult register(@RequestParam("username") String username, @RequestParam("password") String password) {

        User user = userService.checkUserName(username.trim());
        if (!isEmpty(user)) {
            return CommonResult.paramFailed("用户名重复");
        }
        int i = userService.registerUser(username.trim(), password.trim());
        if (i == 1) {
            return CommonResult.success("注册成功!!!");
        }else {
            return CommonResult.failed();
        }
    }

//    @PostMapping("login")
//    private CommonResult login(@RequestParam("username") String username, @RequestParam("password")String password){
//
//
//        new
//        return CommonResult("jj","jj",);
//    }


}
